package com.chen.palmar.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chen.palmar.R;
import com.flyco.dialog.widget.NormalDialog;

import butterknife.ButterKnife;
import dmax.dialog.SpotsDialog;
import rx.subscriptions.CompositeSubscription;

/**
 * Fragment基类
 */
public abstract class BaseFragment extends Fragment {

    public BaseActivity mActivity;
    public CompositeSubscription subscription;
    private SpotsDialog spotsDialog;

    // Fragment被创建
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 获取所在的activity对象
        mActivity = (BaseActivity) getActivity();
        subscription = new CompositeSubscription();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    // activity创建结束
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    /**
     * 布局ID
     */
    public abstract int getLayoutId();

    /**
     * 初始化数据
     */
    public abstract void initData();

    /**
     * 初始化监听器，可以不实现
     */
    public void initListener() {

    }

    public void showToast(String text) {
        Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show();
    }

    public NormalDialog showAlterDialog(String title, String message) {
        NormalDialog dialog = new NormalDialog(mActivity);
        dialog.widthScale(0.7f);
        dialog.style(NormalDialog.STYLE_TWO);
        dialog.titleLineColor(ContextCompat.getColor(mActivity, R.color.colorPrimary));
        dialog.title(title);
        dialog.content(message);
        dialog.dividerColor(ContextCompat.getColor(mActivity, R.color.line));
        dialog.show();
        return dialog;
    }

    public NormalDialog showAlterDialogOne(String title, String message) {
        NormalDialog dialog = new NormalDialog(mActivity);
        dialog.style(NormalDialog.STYLE_TWO);
        dialog.btnNum(1);
        dialog.widthScale(0.7f);
        dialog.titleLineColor(ContextCompat.getColor(mActivity, R.color.colorPrimary));
        dialog.title(title);
        dialog.btnText("确定");
        dialog.content(message);
        dialog.dividerColor(ContextCompat.getColor(mActivity, R.color.line));
        dialog.show();
        return dialog;
    }

    public SpotsDialog showLoading(String text) {
        if (null == spotsDialog) {
            spotsDialog = new SpotsDialog(getContext(), text);
        }
        spotsDialog.show();
        return spotsDialog;
    }

    public void hideLoading(String text) {
        if (null != spotsDialog) {
            spotsDialog.dismiss();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        if (null != subscription) {
            subscription.clear();
            subscription.unsubscribe();
        }
    }
}
